package com.qingfeng.coupon.vo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.qingfeng.base.exception.LsException;
import com.qingfeng.base.util.CodeEnum;
import com.qingfeng.coupon.bean.CouponDistributeGoods;
import com.qingfeng.coupon.bean.CouponTemplate;
import com.qingfeng.coupon.bean.CouponUseGoods;
import com.qingfeng.coupon.constant.CouponCategory;
import com.qingfeng.coupon.constant.CouponDistributeType;
import com.qingfeng.coupon.constant.CouponExpireType;
import com.qingfeng.coupon.constant.CouponUseType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/5/26 3:21 下午
 */
@ApiModel(value = "模版添加vo")
@Data
public class CouponTemplateVo {

    private Long id;
    /**
     * 优惠券分类
     */
    @ApiModelProperty(value = "优惠券分类",example = "1",required = true)
    private Integer category;

    /**
     * 优惠券名称
     */
    @ApiModelProperty(value = "优惠券名称",example = "test-name",required = true)
    private String name;


    /**
     * 优惠券logo
     */
    @ApiModelProperty(value = "logo",example = "test-name",required = false)
    private String logo;

    /**
     * 总数
     */
    @ApiModelProperty(value = "总数",example = "100",required = true)
    private Integer count;



    /**
     * 优惠券描述
     */
    @ApiModelProperty(value = "优惠券描述",required = false)
    private String intro;

    /**
     * 分发场景，1单商品，2达到总金额
     */
    @ApiModelProperty(value = "分发场景，1单商品，2达到总金额",example = "1",required = true)
    private Integer distributeType;

    /**
     * 每人最多可领取几张
     */
    @ApiModelProperty(value = "每人最多可领取几张",example = "1",required = true)
    private Integer distributeUserLimite;

    /**
     * 每天限领多少张
     */
    @ApiModelProperty(value = "每天限领多少张",example = "10",required = false)
    private Integer distributeDayLimite;



    /**
     * 分发最低购买金额
     */
    @TableField(value = "distribute_base")
    @ApiModelProperty(value = "分发最低购买金额/分",example = "100",required = true)
    private Integer distributeBase;

    /**
     * 开始分发时间
     */
    @ApiModelProperty(value = "开始分发时间",required = true)
    private Date distributeStart;

    /**
     * 结束分发时间
     */
    @ApiModelProperty(value = "结束分发时间",required = true)
    private Date distributeEnd;

    /**
     * 有效期规则，1固定日期，2固定时间长度
     */
    @ApiModelProperty(value = "有效期规则，1固定天数，2固定日期范围",example = "1",required = true)
    private Integer expirePeriod;

    /**
     * 有效期间隔，单位天数
     */
    @ApiModelProperty(value = "有效期间隔，单位天数",example = "1",required = false)
    private Integer expireGap;

    /**
     * 有效期开始时间
     */
    @ApiModelProperty(value = "有效期开始时间")
    private Date expireStart;

    /**
     * 有效期结束时间
     */
    @ApiModelProperty(value = "有效期结束时间")
    private Date expireEnd;

    /**
     * 使用规则-规则类型，1指定金额满减，2指定消费商品类型满减，3指定消费商品满减,
     */
    @ApiModelProperty(value = "使用规则-规则类型，" +
            "1指定金额满减，" +
            "2指定消费商品类型满减，" +
            "3指定消费商品满减,",required = true,example = "1")
    private Integer useType;


    /**
     * 满减额度,减20
     */
    @ApiModelProperty(value = "满减额度,减20",example = "2000",required = true)
    private Integer discountQuota;

    /**
     * 满减基准
     */
    @ApiModelProperty(value = "使用门槛",example = "10000",required = true)
    private Integer discountBase;



    @ApiModelProperty(value = "分发商品",required = false)
    private transient List<CouponDistributeGoods> distributeGoods;



    @ApiModelProperty(value = "可用优惠券商品",required = false)
    private transient List<CouponUseGoods> useGoods;


    public CouponTemplate toCouponTemplate(){
        CouponTemplate template = new CouponTemplate();
        template.setCategory(CouponCategory.of(this.getCategory()));
        template.setName(this.getName());
        template.setLogo(this.getLogo());
        template.setCount(this.getCount());
        template.setUseCount(0);
        template.setExpireCount(0);
        template.setDistributeCount(0);
        template.setIntro(this.getIntro());
        template.setAvailable(false);
        template.setExpired(false);
        template.setProductline(1);
        if (getDistributeType()!=null){
            template.setDistributeType(CouponDistributeType.of(this.getDistributeType()));
        }
        template.setDistributeUserLimite(this.getDistributeUserLimite());
        template.setDistributeDayLimite(this.getDistributeDayLimite());
        template.setDistributeBase(this.getDistributeBase());
        template.setDistributeStart(this.getDistributeStart());
        template.setDistributeEnd(this.getDistributeEnd());
        template.setExpirePeriod(CouponExpireType.of(this.getExpirePeriod()));
        template.setExpireStart(this.getExpireStart());
        template.setExpireEnd(this.getExpireEnd());
        template.setExpireGap(this.getExpireGap());
        template.setUseType(CouponUseType.of(this.getUseType()));
        template.setDiscountQuota(this.getDiscountQuota());
        template.setDiscountBase(this.getDiscountBase());
        template.setWeight(1);
        template.setDistributeGoods(this.getDistributeGoods());
        template.setUseGoods(this.getUseGoods());
        return template;
    }


    public CouponTemplate toCouponTemplate2(){
        CouponTemplate template = new CouponTemplate();
        template.setId(this.getId());
        template.setCategory(CouponCategory.of(this.getCategory()));
        template.setName(this.getName());
        template.setLogo(this.getLogo());
        template.setCount(this.getCount());
        template.setIntro(this.getIntro());
        template.setAvailable(false);
        template.setExpired(false);
        if (getDistributeType()!=null){
            template.setDistributeType(CouponDistributeType.of(this.getDistributeType()));
        }
        template.setDistributeUserLimite(this.getDistributeUserLimite());
        template.setDistributeDayLimite(this.getDistributeDayLimite());
        template.setDistributeBase(this.getDistributeBase());
        template.setDistributeStart(this.getDistributeStart());
        template.setDistributeEnd(this.getDistributeEnd());
        if (getExpirePeriod()!=null) {
            template.setExpirePeriod(CouponExpireType.of(this.getExpirePeriod()));
        }
        template.setExpireStart(this.getExpireStart());
        template.setExpireEnd(this.getExpireEnd());
        template.setExpireGap(this.getExpireGap());
        if (this.getUseType()!=null) {
            template.setUseType(CouponUseType.of(this.getUseType()));
        }
        template.setDiscountQuota(this.getDiscountQuota());
        template.setDiscountBase(this.getDiscountBase());
        template.setDistributeGoods(this.getDistributeGoods());
        template.setUseGoods(this.getUseGoods());
        return template;
    }




    /**
     * 校验数据正确性
     * @throws LsException
     */
    public void vaildata() throws LsException {
        if (this.category==null){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"优惠券分类不能为空");
        }
        if (this.category>CouponCategory.BUYER_COUPON.getCode() || this.category<CouponCategory.GOODS_COUPON.getCode()){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"未知的优惠券类型");
        }
        if (this.getCategory().equals(CouponCategory.GOODS_COUPON.getCode())){
            if (this.getDistributeType()==null){
                throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"请指定发放方式");
            }
        }
        if (this.getDiscountQuota() ==null || this.getDiscountQuota()<=0){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"满减金额不能为0");
        }
        if (getExpireEnd() != null && this.getDistributeStart().after(getExpireEnd())){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"请在有效期之前分发");
        }

        if (this.count==null){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"分发数量不能为空");
        }

        if (StringUtils.isEmpty(this.name)){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"优惠券名称不能为空");
        }
        if (StringUtils.isEmpty(this.getName()) || this.name.length()>15){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"优惠券名称不能超过15个字符");
        }
        if (this.count<0){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"发放总量不能为负数");
        }
        if (this.getCategory()==CouponCategory.GOODS_COUPON.getCode()) {
            if (this.distributeType.equals(CouponDistributeType.BUY_GOODS_DISTRIBUTE.getCode())) {
                if (CollectionUtils.isEmpty(this.distributeGoods)) {
                    throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(), "指定分发商品不能为空");
                }
            } else if (this.distributeType.equals(CouponDistributeType.SPECIFIED_AMOUNT_DISTRIBUTE.getCode())) {
                if (this.distributeBase < 0) {
                    throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(), "分发减基准必须大于0");
                }
            }
        }
        if (this.expirePeriod.equals(CouponExpireType.FIX_DEADLINE.getCode())){
            if (this.expireEnd==null || this.expireStart==null){
                throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"请过期范围");
            }
            //如果是固定日期范围，则要判断，有效期在发放时间结束之后
            if (this.distributeEnd.getTime()>this.expireEnd.getTime()){
                throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"必须在有效期之前发放完成");
            }
        }

        //如果不是全场通用，则useGoodsList 不能为空
        if (!this.useType.equals(CouponUseType.AMOUNT_USE.getCode())){
            if (CollectionUtils.isEmpty(this.getUseGoods())){
                throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"使用商品或者分类不能为空");
            }
        }
        if (this.discountBase == null){
            throw new LsException(CodeEnum.PARAM_VALID_ERROR.getCode(),"使用门槛字段必填");
        }
    }

}
